﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WindowsFormsApplication3
{
    using alldataset;
    using alldataset.fullldatasetTableAdapters;

    public partial class NewEmpRegFormcs : Form
        
    {
        
        Transaction.EmployeeTransaction emptransaction = null;
        
        DataBean.EmployeeDatabean empdatabean = null;
        public NewEmpRegFormcs()
        {
            InitializeComponent();
            emptransaction = new Transaction.EmployeeTransaction();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
            

        }
        /// <summary>
        /// validates thr controls
        /// </summary>
        /// <returns></returns>
        public Boolean validationcontrol()
        {
            Boolean sucess = false;
            if (txtname.Text.Trim() == null || txtname.Text.Trim() == "")
            {
                lblStatus.Text = "Enter Name";
                txtname.Focus();
                txtname.Text = "";
            }
            else if (txtDesignation.Text.Trim() == null || txtDesignation.Text.Trim() == "")
            {

                txtDesignation.Focus();
                lblStatus.Text = "Enter an Designation";
                txtDesignation.Text = "";
            }

            else if (txtpassportno.Text.Trim() == null || txtpassportno.Text.Trim() == "")
            {

                txtpassportno.Focus();
                lblStatus.Text = "Enter an valid Passport No#";
                txtpassportno.Text = "";
            }
            else if (mtxtphoneno.Text.Trim() == null || mtxtphoneno.Text.Trim() == "")
            {

                mtxtphoneno.Focus();
                lblStatus.Text = "Enter a Phone no";
                mtxtphoneno.Text = "";
            }
           
            else if (rhtaddress.Text.Trim() == null || rhtaddress.Text.Trim() == "")
            {

                rhtaddress.Focus();
                lblStatus.Text = "Enter an Adress";
                rhtaddress.Text = "";
            }
            else if (mtxtbasic.Text.Trim() == null || mtxtbasic.Text.Trim() == "")
            {

                mtxtbasic.Focus();
                lblStatus.Text = "Enter Basic Salary";
                mtxtbasic.Text = "";
            }
            else if (mtxtTa.Text.Trim() == null || mtxtTa.Text.Trim() == "")
            {

                mtxtTa.Focus();
                lblStatus.Text = "Enter an Travel Allowance";
                mtxtTa.Text = "";
            }
            else if (mtxtHA.Text.Trim() == null || mtxtHA.Text.Trim() == "")
            {

                mtxtHA.Focus();
                lblStatus.Text = "Enter Housing Allowance";
                mtxtHA.Text = "";
            }
            else if (mtxtothers.Text.Trim() == null || mtxtothers.Text.Trim() == "")
            {

                mtxtothers.Focus();
                lblStatus.Text = "Enter Other";
                mtxtothers.Text = "";
            }
            else if (txtvisa.Text.Trim() == null || txtvisa.Text.Trim() == "")
            {

                txtvisa.Focus();
                lblStatus.Text = "Enter Visa No";
                txtvisa.Text = "";
            }
            else if (txtuaeid.Text.Trim() == null || txtuaeid.Text.Trim() == "")
            {

                txtuaeid.Focus();
                lblStatus.Text = "Enter an uae id";
                txtuaeid.Text = "";
            }
            else if (mtxtTotal.Text.Trim() == null || mtxtTotal.Text.Trim() == "")
            {

                mtxtTotal.Focus();
                lblStatus.Text = "Enter salary Datas";
               
            }
            else
            {

                sucess = true;
            }
            return sucess;
        }

        /// <summary>
        /// 
        /// </summary>
        public void insertdatatotable()
        {
            empdatabean = new DataBean.EmployeeDatabean();
           
            empdatabean.Employename= txtname.Text;
            if (rbtfemale.Checked == true)
            {
                empdatabean.Gender = "Female";
            }
            else
            {
                empdatabean.Gender = "Male";
            }
            empdatabean.Designation = txtDesignation.Text;
            empdatabean.Nationality1 = cmbnationality.Text;
            empdatabean.Visano = txtvisa.Text;
            empdatabean.Uaeid = txtuaeid.Text;
            empdatabean.Passportno = txtpassportno.Text;
            empdatabean.Phoneno = mtxtphoneno.Text;
            empdatabean.Adress = rhtaddress.Text;
            empdatabean.Basicsalary = int.Parse( mtxtbasic.Text);
            empdatabean.Ha = int.Parse(mtxtbasic.Text);
            empdatabean.Ta = int.Parse(mtxtbasic.Text);
            empdatabean.Others = int.Parse(mtxtbasic.Text);
            empdatabean.Total = int.Parse(mtxtTotal.Text);
            empdatabean.Joiningdate = dateTimePicker1.Value.Date;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            EmployeedatatableTableAdapter empadapt = new EmployeedatatableTableAdapter();
            empadapt.Connection = new System.Data.OleDb.OleDbConnection(Program.ConnStr);
           
            if (validationcontrol())
            {
                insertdatatotable();
                try
                {
                    emptransaction.insertEmpData(empdatabean);
                    MessageBox.Show("Sucessfully Entered Employee Data");


                   
                    DataTable dt= empadapt.Getemployeepk(empdatabean.Employename,empdatabean.Passportno);
                  
                    
                    int empepk = int.Parse(dt.Rows[0][15].ToString());


                    try
                    {
                        emptransaction.insertempsalarydata(empepk, empdatabean);

                    }
                    catch (Exception expx)
                    {
                       MessageBox.Show("Exception at  emptransaction.insertempsalarydata(empepk, empdatabean);"+expx);
                        if (emptransaction.DeleteEmpData(empepk))
                        {
                       
                        this.Close();
                         }
                    }
                
                
                }
                catch (Exception exp)
                {
                    
                   MessageBox.Show("Exception at  emptransaction.insertEmpData(empdatabean);"+exp);
                    this.Close();
                  
                }
               
            }
            else
            {

            }
        }




 









 







    }
}
